package com.example.administrator.activity.parcitise.Storage.Inner_output;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.activity.R;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class FileInnerActivity extends AppCompatActivity {
   Button file_inner_writer,file_inner_read;
   TextView file_inner_tv;
   String fileName="slsher.txt";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_inner);
        init();
        View.OnClickListener clickListener=new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    case R.id.ch3_file_inner_read:
                        read();
                        break;
                    case R.id.ch3_file_inner_write:
                        writer();
                        break;
                }
            }
        };
        file_inner_writer.setOnClickListener(clickListener);
        file_inner_read.setOnClickListener(clickListener);
        file_inner_tv.setOnClickListener(clickListener);
    }

    /**
     * 从内部文件读取数据，展示在TextView
     * 1.利用Context提供的openFileInput（）方法得到FileInputStream对象
     * 2.利用Java流的方式读取数据到TextView
     */
    private void read() {
        FileInputStream inputStream;
        BufferedReader reader;
        try {
            inputStream=this.openFileInput(fileName);
            reader=new BufferedReader(new InputStreamReader(inputStream));
            String line=null;
            String s="";
            while((line=reader.readLine())!=null){
                s=s+line+"\n";
            }
            file_inner_tv.setText(s);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 将内容写入内部文件：
     * 1.定义文件夹
     * 2.定义要写入文件内容
     * 3.利用Context提供的openFileOutput（）方法得到FileOutputStream对象
     * 4.Java流的方法将数据写入文件
     */
    private void writer() {
        String message="hello\n中国";
        FileOutputStream outputStream;
        BufferedWriter writer;
        try {
            outputStream=this.openFileOutput(fileName,MODE_PRIVATE);
            /**
             * 可以直接使用基本字符流：也可以字符缓冲流
             */
            writer=new BufferedWriter(new OutputStreamWriter(outputStream));
            writer.write(message);
            writer.flush();
            Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            /*
            关闭流
             */
        }

    }

    private void init() {
        file_inner_read=findViewById(R.id.ch3_file_inner_read);
        file_inner_writer=findViewById(R.id.ch3_file_inner_write);
        file_inner_tv=findViewById(R.id.ch3_file_inner_tv);
    }
}
